home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / SHDTO3DV.C < prev    next >
Text File  |  1992-05-26  |  3KB  |  111 lines

  1. /* SHDto3DV.c  -  convert "shaded" format to 3DV */
  2.  
  3. /* Oscar Garcia <garciao@mof.govt.nz>, May 1992 */
  4.  
  5. /* uses getopt */
  6. int getopt(int argc, char *argv[], char *options);
  7. extern  int optind;
  8. extern char *optarg;
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <alloc.h>
  13.  
  14. #define USAGE "usage: SHDto3DV [/cn] [infile] [outfile]\n\
  15. \tIf file names are omitted, the standard i/o streams are used.\n\
  16. \tOptions (with defaults):\n\
  17. \t\t/c9  -  colour\n"
  18.  
  19. #define ERROR(msg) {fputs(msg,stderr),exit(1);}
  20. #define PERROR(msg) {perror(msg),exit(1);}
  21.  
  22. void main(int argc, char* argv[])
  23. {
  24.     int npoints, nfaces, i, n, m, p, *a, colour = 9;
  25.     unsigned long memory;
  26.     FILE *infile = stdin, *outfile = stdout;
  27.     char line[82];
  28.     char options[] = "c:";
  29.  
  30.     /* get options */
  31.     while ((n = getopt(argc, argv, options)) != EOF)
  32.     {    if (n == '?')
  33.             ERROR(USAGE)
  34.         else
  35.             colour = atoi(optarg);
  36.     }
  37.  
  38.     /* open files */
  39.     if (optind < argc - 2)
  40.         ERROR(USAGE);    /* too many */
  41.     if (optind < argc)
  42.     {    infile = fopen(argv[optind], "rt");
  43.         if (infile == NULL)
  44.             ERROR("Can't open input file");
  45.         if (++optind < argc)
  46.         {    outfile = fopen(argv[optind], "wt");
  47.             if (outfile == NULL)
  48.                 ERROR("Can't open output file");
  49.         }
  50.     }
  51.  
  52.     /* first line (counts) */
  53.     if (2 != fscanf(infile, "%d %d", &npoints, &nfaces) ||
  54.             NULL == fgets(line,81,infile))
  55.         ERROR("error reading first line");
  56.     fprintf(outfile, "%d\n", npoints);
  57.  
  58.     /* skip second line (limits) */
  59.     if (NULL == fgets(line, 81, infile))
  60.         ERROR("error reading second line");
  61.  
  62.     /* copy coordinates */
  63.     for (i = 1; i <= npoints; i++)
  64.     {    if (NULL == fgets(line, 81, infile))
  65.         {    fprintf(stderr, "error reading point #%d", i);
  66.             exit(1);
  67.         }
  68.         fputs(line, outfile);
  69.     }
  70.  
  71.     /* process faces */
  72.     memory = coreleft() - 999;
  73. #ifdef __MSDOS__
  74.     if (memory > 0xfff0U)
  75.         memory = 0xfff0U;
  76. #endif
  77.     memory = memory / sizeof(int);
  78.     a = (int*) malloc(memory * sizeof(int));
  79.     if (a == NULL)
  80.         ERROR("memory allocation failure");
  81.     n = 0;
  82.     for (n = 0; nfaces > 0 && n < memory; nfaces--)
  83.     {    fscanf(infile, "%d %d", &m, &p);
  84.         a[n++] = -p;    /* flag move with - */
  85.         while (--m > 0)
  86.             fscanf(infile, "%d", &a[n++]);
  87.         a[n++] = p;        /* close the face */
  88.     }
  89.     if (n >= memory)
  90.         ERROR("out of memory");
  91.     if (ferror(infile))
  92.         PERROR("error reading file");
  93.     if (feof(infile))
  94.         ERROR("unexpected end of file on input");
  95.     fclose(infile);
  96.  
  97.  
  98.     /* output lines */
  99.     fprintf(outfile, "%d\n", n);
  100.     for (i = 0; i < n; i++)
  101.     {    if (a[i] < 0)
  102.             fprintf(outfile, "%d %d\n", -a[i], 0); /* move */
  103.         else
  104.             fprintf(outfile, "%d %d\n", a[i], colour); /* draw */
  105.     }
  106.     if (ferror(outfile))
  107.         PERROR("eror writing output");
  108.     fclose(outfile);
  109.  
  110. }
  111.